'use strict';
const dayjs = require("dayjs");
const { Controller } = require('egg');
const { Op } = require('sequelize');

class HomeController extends Controller {

    async dashboard() {
        const { start_time, end_time } = this.ctx.query;
        const timeQuery = {};

        if (start_time && end_time) {
            timeQuery.create_time = {
                [Op.between]: [new Date(start_time), new Date(end_time)]
            };
        }

        /**
         * 根据日志汇总每个国家的占比
         */
        let logList = await this.service.apiLog.allList(timeQuery);
        // 统计country
        let countryMap = {};
        for (let item of logList) {
            if (countryMap[item.country]) {
                countryMap[item.country] += 1;
            } else {
                countryMap[item.country] = 1;
            }
        }
        let countryList = [];
        for (let key in countryMap) {
            countryList.push({
                name: key,
                value: countryMap[key]
            })
        }

        // 查询订单列表
        const orderTimeQuery = {
            status: {
                [Op.ne]: -1
            }
        };

        if (start_time && end_time) {
            orderTimeQuery.pay_time = {
                [Op.between]: [new Date(start_time), new Date(end_time)]
            };
        }

        let totalOrderList = await this.service.order.allList(orderTimeQuery);
        let userOrderList = totalOrderList.filter(order => order.role === 1)
        let companyOrderList = totalOrderList.filter(order => order.role === 2)

        // 用户订单国家占比
        let userCountryMap = {};
        for (let item of userOrderList) {
            if (userCountryMap[item.country]) {
                userCountryMap[item.country] += 1;
            } else {
                userCountryMap[item.country] = 1;
            }
        }
        let userCountryList = [];
        for (let key in userCountryMap) {
            userCountryList.push({
                name: key,
                value: userCountryMap[key]
            })
        }

        // 公司订单与散客订单占比
        let compareList = [
            {
                name: '散客',
                value: userOrderList.length
            },
            {
                name: '代理商',
                value: companyOrderList.length
            }
        ];

        // 按日期统计访问量，相同IP在一天内访问算一次
        let dateMap = {};
        for (let item of logList) {
            let date = dayjs(item.create_time).format('YYYY-MM-DD');
            if (dateMap[date]) {
                if (!dateMap[date].includes(item.ip)) {
                    dateMap[date].push(item.ip);
                }
            } else {
                dateMap[date] = [item.ip];
            }
        }
        let dateList = [];
        for (let key in dateMap) {
            dateList.push({
                name: key,
                value: dateMap[key].length
            })
        }
        // 按日期统计订单量
        let orderMap = {};
        for (let item of totalOrderList) {
            let date = dayjs(item.pay_time).format('YYYY-MM-DD');
            if (orderMap[date]) {
                orderMap[date] += 1;
            } else {
                orderMap[date] = 1;
            }
        }
        let orderDateList = [];
        for (let key in orderMap) {
            orderDateList.push({
                name: key,
                value: orderMap[key]
            })
        }

        // 按日期排序
        const sortByDate = (a, b) => new Date(a.name) - new Date(b.name);
        dateList = dateList.filter(item => item.name !== 'Invalid Date').sort(sortByDate);
        orderDateList = orderDateList.filter(item => item.name !== 'Invalid Date').sort(sortByDate);

        this.ctx.success({
            countryList,
            userCountryList,
            compareList,
            dateList,
            orderDateList,
        });
    }

}

module.exports = HomeController;
